package com.dahaiwuliang.code.server.impl;

import com.dahaiwuliang.code.dao.CommentDao;
import com.dahaiwuliang.code.pojo.Comment;
import com.dahaiwuliang.code.server.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

/**评论service实现
 * @author bzd
 */
@Service("commentService")
public class CommentServiceImpl implements CommentService {
   @Autowired
   private CommentDao commentDao;
    @Override
    public void save(Comment comment) {
        commentDao.save(comment);
    }

    @Override
    public Page<Comment> List(Comment s_comment, Integer page, Integer pageSize, Sort.Direction direction, String... properties) {


        return commentDao.findAll(new Specification<Comment>() {
            @Override
            public Predicate toPredicate(Root<Comment> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Predicate predicate=criteriaBuilder.conjunction();
                if (s_comment!=null){
                    if (s_comment.getState()!=null){
                        predicate.getExpressions().add(criteriaBuilder.equal(root.get("state"),s_comment.getState()));
                    }
                    if (s_comment.getArticle()!=null&&s_comment.getArticle().getArticleId()!=null){
                        predicate.getExpressions().add(criteriaBuilder.equal(root.get("article").get("articleId"),s_comment.getArticle().getArticleId()));
                    }
                }
                return predicate;
            }
        },PageRequest.of(page-1,pageSize,direction,properties));
    }

    @Override
    public Long getTotal(Comment comment) {
        return commentDao.count(new Specification<Comment>() {
            @Override
            public Predicate toPredicate(Root<Comment> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Predicate predicate=criteriaBuilder.conjunction();
                if (comment!=null){
                    if (comment.getState()!=null){
                        predicate.getExpressions().add(criteriaBuilder.equal(root.get("state"),comment.getState()));
                    }
                    if (comment.getArticle()!=null&&comment.getArticle().getArticleId()!=null){
                        predicate.getExpressions().add(criteriaBuilder.equal(root.get("article").get("articleId"),comment.getArticle().getArticleId()));
                    }
                }
                return predicate;
            }
        });
    }

    @Override
    public void delete(Integer id) {
        commentDao.deleteById(id);
    }

    @Override
    public Comment get(Integer id) {
        return commentDao.getOne(id);
    }

    @Override
    public void deleteByArticleId(Integer articleId) {
        commentDao.deleteByArticleId(articleId);
    }
}
